package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetModel;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.domain.frock.FrockSeat;
import com.ruoyi.system.mapper.BaseShopMapper;
import com.ruoyi.system.mapper.SetModelMapper;
import com.ruoyi.system.mapper.frock.FrockSeatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IFrockSeatService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工装管理基础Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-20
 */
@Service
public class FrockSeatServiceImpl extends ServiceImpl<FrockSeatMapper, FrockSeat> implements IFrockSeatService {

    @Autowired
    private SetModelMapper setModelMapper;
    @Autowired
    private BaseShopMapper baseShopMapper;

    @Override
    public List<FrockSeat> selectList(FrockSeat frockSeat) {
        LambdaQueryWrapper<FrockSeat> lqw = new LambdaQueryWrapper<FrockSeat>();
        if (StringUtils.isNotBlank(frockSeat.getFrockCode())){
            lqw.eq(FrockSeat::getFrockCode ,frockSeat.getFrockCode());
        }
        if (StringUtils.isNotBlank(frockSeat.getFrockName())){
            lqw.like(FrockSeat::getFrockName ,frockSeat.getFrockName());
        }
        if (StringUtils.isNotBlank(frockSeat.getFrockSpes())){
            lqw.eq(FrockSeat::getFrockSpes ,frockSeat.getFrockSpes());
        }
        if (StringUtils.isNotBlank(frockSeat.getFactory())){
            lqw.eq(FrockSeat::getFactory ,frockSeat.getFactory());
        }
        if (StringUtils.isNotBlank(frockSeat.getSize())){
            lqw.eq(FrockSeat::getSize ,frockSeat.getSize());
        }
        if (frockSeat.getRecordDate() != null){
            lqw.eq(FrockSeat::getRecordDate ,frockSeat.getRecordDate());
        }
        if (StringUtils.isNotBlank(frockSeat.getProductId())){
            lqw.eq(FrockSeat::getProductId ,frockSeat.getProductId());
        }
        if (StringUtils.isNotBlank(frockSeat.getShopId())){
            lqw.eq(FrockSeat::getShopId ,frockSeat.getShopId());
        }
        if (frockSeat.getLife() != null){
            lqw.eq(FrockSeat::getLife ,frockSeat.getLife());
        }
        if (frockSeat.getNumber() != null){
            lqw.eq(FrockSeat::getNumber ,frockSeat.getNumber());
        }
        if (frockSeat.getStatus() != null){
            lqw.eq(FrockSeat::getStatus ,frockSeat.getStatus());
        }

        List<FrockSeat> frockSeats = baseMapper.selectList(lqw);
        if(IterUtil.isNotEmpty(frockSeats)){
            frockSeats.stream().forEach(i -> {
                //根据产品id查询出产品名称
                List<Long> list = Arrays.stream(i.getProductId().split(","))
                .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                i.setProductIds(list);
                if(IterUtil.isNotEmpty(list)){
                    List<SetModel> listModel = setModelMapper.selectList(new QueryWrapper<SetModel>().in("id", list));
                    if(IterUtil.isNotEmpty(listModel)){
                        i.setProductNames(listModel.stream().map(j->j.getModel()).collect(Collectors.toList()));
                    }
                }

                //根据工位id查出工位名称
                List<Long> listShopIds = Arrays.stream(i.getShopId().split(","))
                        .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                i.setShopIds(listShopIds);
                if(IterUtil.isNotEmpty(listShopIds)){
                    List<BaseShop> listShop = baseShopMapper.selectList(new QueryWrapper<BaseShop>().in("shop_id", listShopIds));
                    if(IterUtil.isNotEmpty(listShop)){
                        i.setShopNames(listShop.stream().map(j->j.getShopName()).collect(Collectors.toList()));
                    }
                }


            });
        }

        return frockSeats;
    }

    @Override
    public FrockSeat getByIdFr(Long frockId) {
        FrockSeat frockSeat = baseMapper.selectById(frockId);
        if(ObjectUtil.isNotNull(frockSeat)){
            //根据产品id查询出产品名称
            List<Long> list = Arrays.stream(frockSeat.getProductId().split(","))
                    .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
            frockSeat.setProductIds(list);
            if(IterUtil.isNotEmpty(list)){
                List<SetModel> listModel = setModelMapper.selectList(new QueryWrapper<SetModel>().in("id", list));
                if(IterUtil.isNotEmpty(listModel)){
                    frockSeat.setProductNames(listModel.stream().map(j->j.getModel()).collect(Collectors.toList()));
                }
            }

            //根据工位id查出工位名称
            List<Long> listShopIds = Arrays.stream(frockSeat.getShopId().split(","))
                    .mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
            frockSeat.setShopIds(listShopIds);
            if(IterUtil.isNotEmpty(listShopIds)){
                List<BaseShop> listShop = baseShopMapper.selectList(new QueryWrapper<BaseShop>().in("shop_id", listShopIds));
                if(IterUtil.isNotEmpty(listShop)){
                    frockSeat.setShopNames(listShop.stream().map(j->j.getShopName()).collect(Collectors.toList()));
                }
            }
        }
        return frockSeat;
    }

    @Override
    public boolean saveFr(FrockSeat frockSeat) {
        boolean bo = true;
        if(ObjectUtil.isNotNull(frockSeat)){
            List<Long> productIds = frockSeat.getProductIds();
            frockSeat.setProductId(productIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
            List<Long> shopIds = frockSeat.getShopIds();
            frockSeat.setShopId(shopIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        frockSeat.setStatus(1);//新建
        int insert = baseMapper.insert(frockSeat);
        if(insert<0){
            bo=false;
        }
        return bo;
    }

    @Override
    public boolean updateByIdFr(FrockSeat frockSeat) {
        boolean bo = true;
        if(ObjectUtil.isNotNull(frockSeat)){
            List<Long> productIds = frockSeat.getProductIds();
            frockSeat.setProductId(productIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
            List<Long> shopIds = frockSeat.getShopIds();
            frockSeat.setShopId(shopIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        int insert = baseMapper.updateById(frockSeat);
        if(insert<0){
            bo=false;
        }
        return bo;
    }
}
